import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.ArrayList; 
import oracle.jdbc.pool.OracleDataSource;
import java.util.Date.*;
import java.text.*;

public class addMarket extends HttpServlet
{
	public static dbapi conn;
	// Display form for adding house to market
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{ 
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println(htmlHeader.header);
		conn = new dbapi();
		// Connect to Oracle
		conn.connect();
		
		// Get id for house
		String id = request.getParameter("id");
		
		// Display form
		out.println(
		"<img src=\"http://kpbs.media.clients.ellingtoncms.com/img/croppedphotos/2009/05/14/house-sale_t614.jpg?a3ca5463f16dc11451266bb717d38a6025dcea0e\" width=\"150\" height=\"150\" border=\"0\" alt=\"Market\" />"+
		"<p style=\"font-size:20pt;\">Add House to Market</p>"+
		"<html><head></head>"+
		"<body>"+
		"<form name=\"addMarket\" action=\"/project/addMarket\" method=\"post\">"+
		"<table align=center><input type=\"hidden\" name=\"HOMEID\" value="+id+">"+
		"<tr><td>Price:</td>"+
		"<td><input type=\"text\" name=\"PRICE\"></td>&nbsp&nbsp");
		
		// Error if servlet cannot connect to Oracle
		if(!conn.connected)
		{
			out.println("SQL ERROR CANNOT CONNECT TO DATABASE!<br>");
			out.println(conn.msg + "<br>");
			return;
		}

		// Get agent list for dropdown
		ArrayList<String> agentList = conn.getAgents();
		// If agentlist is null return error
		if(agentList == null){
			out.println("Error: null resultset from database<br>"+conn.msg);
			return;	
		}else{
			out.println("<table align=center>");
			out.println("Select an Agent:");
			out.println("<select name=\"AgentID\" style=\"height: 20px; width: 175px\">");
			// Put agents in dropdown
			for(int i=0;i<agentList.size();i += 2)
			{
				
				out.println("<option value=");
				out.println(agentList.get(i));
				out.println("\">");
				out.println(agentList.get(i+1));
				out.println("</option>");
			}	
			out.println("</tr></table>");
		}
		
		out.println(
		"</table>"+
		"<input type=\"submit\">"+
		"</form>"+
		"<body></html>");
	}
	
	// Put house on market
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		conn = new dbapi();
		// Connect to Oracle
		conn.connect();
		out.println(htmlHeader.header);
		// Error if servlet cannot connect to Oracle
		if(!conn.connected)
		{
			out.println("CANT CONNECT! <br>");
			out.println(conn.msg + "<br>");
			return;
		}
		// Create todays date in java
		java.util.Date d = new java.util.Date();		
		// Get variables from form
		String homeID = request.getParameter("HOMEID");
		String AgentID = request.getParameter("AgentID");
		String Price = request.getParameter("PRICE");
		// construct date
		String m =  Integer.toString(d.getMonth()+1); 
		String da =  Integer.toString(d.getDate());
		String y =  Integer.toString(d.getYear()+1900);

		// dbapi call to add house to market
		boolean worked = conn.addMarket(
		Integer.parseInt(homeID), 
		Integer.parseInt(AgentID), 
		Integer.parseInt(Price), 
		m+"/"+da+"/"+y);
		
		// Error if dbapi could not add house to market
		// or refresh to market on success
		if (!worked)
		{
			out.println(conn.msg);
		}else{
			out.println("<header><meta http-equiv=\"refresh\" content=\"2;getMarket\"></header>Added Home To Market...");
		}
		return;
	

	}
}
